﻿var $teacher_course_table;
var $student_score_table;
var is_click_submit_button = false;

window.onload = function () {
  initTeacherCourse();
}

var teacher_course_test_data = {
  total: "6",
  rows: [{
    cour_id: "1",
    cour_title: "计算机组成原理",
  }, {
    cour_id: "2",
    cour_title: "C++程序设计",
  }, {
    cour_id: "3",
    cour_title: "Java程序设计",
  }, {
    cour_id: "4",
    cour_title: "PHP程序设计",
  }, {
    cour_id: "5",
    cour_title: "Go程序设计",
  }, {
    cour_id: "6",
    cour_title: "Vue程序设计",
  },]
}

var student_score_test_data = {
  total: "1",
  rows: [{
    stu_id: "1",					    //学生id
    stu_name: "张三",
    daily_grade: "100",			  //平时成绩
    terminal_grade: "90",		//期末考试成绩
    ultimate_grade: "94",	  //最终成绩是根据配比得来的
    submit_flag: "1",
    proportion: "0.6",				//考试成绩占比
  }, {
    stu_id: "2",					    //学生id
    stu_name: "王五",
    daily_grade: "100",			  //平时成绩
    terminal_grade: "90",		//期末考试成绩
    ultimate_grade: "94",	  //最终成绩是根据配比得来的
    submit_flag: "1",
    proportion: "0.6",				//考试成绩占比
  }, {
    stu_id: "5",					    //学生id
    stu_name: "李四",
    daily_grade: "100",			  //平时成绩
    terminal_grade: "80",		//期末考试成绩
    ultimate_grade: "88",	  //最终成绩是根据配比得来的
    submit_flag: "0",
    proportion: "0.6",				//考试成绩占比
  },],
}

//初始化教师课程表 TODO 获取指定教师号的
function initTeacherCourse() {
  $teacher_course_table = $('#teacher-course-table').bootstrapTable({
    url:"cotro/teacher/getAllTeacherCourseNew.ashx",
    method:"post",
    //data: teacher_course_test_data.rows,
    search: true,
    striped: true,
    searchAlign: "left",
    sidePagination: "client",
    pageList: [5, 25, 50, 100],
    pagination: true,
    height: 450,
    singleSelect: false,
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    queryParams: function (params) {
      params.tea_id = $.cookie('indiv_id');
      //params.tea_id = 20002;
      return params;
    },

    columns: [{
      field: "cour_id",
      title: "课程号"
    }, {
      field: "cour_title",
      title: "课程名"
    }, {
      field: 'cour_id',
      title: '可供操作',
      width: 200,
      align: 'center',
      valign: 'middle',
      formatter: courseDetailFormatter,
    },]
  });
}

function courseDetailFormatter(value, row, index) {
  let result = `
        <button type="button" class="btn btn-success" onclick="detailedCourseScore(${row.cour_id})" data-toggle="modal" data-target="#student_score_modal">详细信息</button>
    `;
  return result;
}

function detailedCourseScore(cour_id) {
  //首先销毁
  $('#student_score_table').bootstrapTable('destroy');
  //TODO 获取学生打分表信息
  $student_score_table = $('#student_score_table').bootstrapTable({
    url:"cotro/teacher/getStudentScoreByCT.ashx",
    method:"post",
    //data: student_score_test_data.rows,
    search: true,
    striped: true,
    searchAlign: "left",
    sidePagination: "client",
    pageList: [5, 25, 50, 100],
    pagination: true,
    height: 450,
    singleSelect: false,
    contentType: "application/x-www-form-urlencoded; charset=UTF-8",
    queryParams: function (params) {
      params.cour_id = cour_id;
      params.tea_id = $.cookie('indiv_id');
      //params.tea_id = 20002;
      return params;
    },
    onClickRow: function (row, $element) {
      if (is_click_submit_button) {
        // 获取相关数据
        stu_id = $element[0].childNodes[0].innerHTML;
        cour_id = cour_id
        daily_grade = $element[0].childNodes[2].childNodes[0].value;
        terminal_grade = $element[0].childNodes[3].childNodes[0].value;
        ultimate_grade = $element[0].childNodes[4].innerHTML;

        // 提交成绩至后台
        $.post("cotro/teacher/updateStudentSC.ashx", {
          stu_id: stu_id,
          cour_id: cour_id,
          daily_grade: daily_grade,
          terminal_grade: terminal_grade,
          ultimate_grade: ultimate_grade,
        }, function (res, status) {
          console.log(res);
          //TODO 如果修改成功的话，将按钮设置为禁止修改
          res = JSON.parse(res);
          if (res.line == "1") {
            alert("打分成功");
            $element[0].childNodes[5].innerHTML = `<button type="button" class="btn btn-secondary" disabled="true">已打分</button>`;
            $student_score_table.bootstrapTable('refresh');
          }
        })
      }
      is_click_submit_button = false;
    },
    columns: [{
      field: "stu_id",
      title: "学生号",
      width: 250,
    }, {
      field: "stu_name",
      title: "学生姓名",
      width: 250,
    }, {
      field: "daily_grade",
      title: "平时成绩",
      width: 250,
      formatter: daily_grade_Formatter,
    }, {
      field: 'terminal_grade',
      title: '期末成绩',
      width: 250,
      formatter: terminal_grade_Formatter,
    }, {
      field: 'ultimate_grade',
      title: '最终成绩',
      class: 'ultimate_grade_box',
      width: 250,
    }, {
      field: 'cour_id',
      title: '可供操作',
      width: 250,
      align: 'center',
      valign: 'middle',
      formatter: markingToolsFormatter,
    },]
  });

}


function markingToolsFormatter(value, row, index) {
  if (row.submit_flag == "1") {
    result = `
        <button type="button" class="btn btn-success" onclick="click_submit_button()">打分</button>
    `;
  } else {
    result = `
        <button type="button" class="btn btn-secondary" disabled="true">已打分</button>
    `;
  }
  return result;
}

//同步锁
function click_submit_button() {
  is_click_submit_button = true;
}

function daily_grade_Formatter(value, row, index) {
  //未提交则返回一个可以修改的输入框
  if (row.submit_flag == "1") {
    result = '<input type="text" class="form-control daily_grade_input" aria-describedby="none" oninput="reCaculate_ultimate_grade(' + index + ')" value="' + row.daily_grade + '"/>'
  } else {
    result = '<input type="text" class="form-control daily_grade_input" aria-describedby="none" disabled="true" value="' + row.daily_grade + '"/>'
  }
  return result;
}

function terminal_grade_Formatter(value, row, index) {
  //未提交则返回一个可以修改的输入框
  if (row.submit_flag == "1") {
    result = '<input type="text" class="form-control terminal_grade_input"  aria-describedby="none" oninput="reCaculate_ultimate_grade(' + index + ')" value="' + row.terminal_grade + '"/>'
  } else {
    result = '<input type="text" class="form-control terminal_grade_input"  aria-describedby="none"  disabled="true" value="' + row.terminal_grade + '"/>'
  }
  return result;
}

//重新计算填写后成绩
function reCaculate_ultimate_grade(index) {
  proportion = $student_score_table.bootstrapTable('getData')[index].proportion;
  let daily_grade_input = document.getElementsByClassName("daily_grade_input");
  let terminal_grade_input = document.getElementsByClassName("terminal_grade_input");
  let ultimate_grade_box = document.getElementsByClassName("ultimate_grade_box");
  ultimate_grade_box[index + 2].innerHTML = daily_grade_input[index].value * (1 - proportion) + terminal_grade_input[index].value * proportion;
}